<?php

class gallery extends fieldset
{

   public function  __construct($superclass = NULL)
   {
      parent::__construct('gallery', $superclass);
   }

   public function getNewImages($type, $limit = 5)
   {
      $this->fields = NULL;
      $this->_data  = NULL;
      $FIELDS       = fields::getInstance();
      $DB           = database::getInstance();
      $params       = array();

      $params['belong']   = 'a';
      $params['node']     = 0;
      $params['active']   = TRUE;
      $params['fieldset'] = $this->name;

      $fieldset = $FIELDS->splitFields($params);
      foreach($fieldset AS $f)
      {
         $this->fields[(!empty($f->db) ? $f->db : $f->alias)] = $f;
      }


      $sql = 'SELECT DISTINCT `id` FROM `meccano_'. $this->name .'`';
      if(!empty($type))
      {
         $KERNEL = kernel::getInstance();

         $type  = explode(',', $type);
         $type  = array_map('trim', $type);
         $types = $KERNEL->getTypes('g');
         $type  = array_intersect($types, $type);

         if(!empty($type))
         {
            $type = array_keys($type);
            $tmp  = array();
            for($i=0, $t=sizeof($type); $i<$t; $i++)
            {
               $tmp[] = 'FIND_IN_SET('. $type[$i] .', `gtypes`)';
            }
            $sql .= ' WHERE '. implode(' OR ', $tmp);
         }
      }
      $sql .= ' ORDER BY `cdate` DESC';
      if(!empty ($limit) && intval($limit))
      {
         $sql .= ' LIMIT 0, '. intval($limit);
      }

      $DB->sql = $sql;
      $ids = $DB->DB_GetFlat();
      if(empty($ids)) return FALSE;
      
      
      $sql  = 'SELECT * FROM `meccano_'. $this->name .'` WHERE `id` IN ('. implode(',', $ids) .') ORDER BY `cdate` DESC';
      $DB->sql = $sql;
      
      $results = $DB->DB_Fetch();
      for($i=0, $t=sizeof($results); $i<$t; $i++)
      {
         $this->addFromSource($results[$i]);
      }

      return $this->getData();
   }
}